﻿using System;

namespace Kugar.Core.BaseStruct
{
    public interface IEmptyResultReturn
    {
        /// <summary>
        ///     返回代码,自定义
        /// </summary>
        int ReturnCode { set; get; }

        /// <summary>
        ///     返回的文本提示信息
        /// </summary>
        string Message { set; get; }

        /// <summary>
        ///     表明函数执行是否成功
        /// </summary>
        bool IsSuccess { get; }

        /// <summary>
        ///     执行过程中出现错误时的错误对象
        /// </summary>
        Exception Error { set; get; }
    }

    public interface IResultReturn:IEmptyResultReturn
    {


        /// <summary>
        ///     如果函数有返回值,则设置该属性
        /// </summary>
        Object ReturnData { set; get; }
    }

    public class EmptyResultReturn : IEmptyResultReturn
    {
        private string _message = string.Empty;

        public EmptyResultReturn(bool isSuccess, string message = "", int returnCode = 0, Exception error = null)
        {
            ReturnCode = returnCode;
            _message = message;
            IsSuccess = isSuccess;
            Error = error;
        }

        public EmptyResultReturn(Exception error)
        {
            Message = error.Message;
            IsSuccess = false;
        }

        /// <summary>
        ///     返回代码,自定义
        /// </summary>
        public int ReturnCode { set; get; }

        /// <summary>
        ///     返回的文本提示信息
        /// </summary>
        public string Message
        {
            set { _message = value; }
            get
            {
                if (string.IsNullOrWhiteSpace(_message))
                {
                    if (Error!=null)
                    {
                        return Error.Message;
                    }
                }
                else
                {
                    return _message;
                }

                return string.Empty;
            }
        }

        /// <summary>
        ///     表明函数执行是否成功
        /// </summary>
        public bool IsSuccess {protected  set; get; }

        /// <summary>
        ///     执行过程中出现错误时的错误对象
        /// </summary>
        public Exception Error { set; get; }    }

    /// <summary>
    ///     函数执行后的返回值
    /// </summary>
    [Serializable]
    public class ResultReturn : IResultReturn
    {
        private string _message = string.Empty;

        public ResultReturn(bool isSuccess, object returnData = null, string message = "", int returnCode = 0, Exception error = null)
        {
            ReturnCode = returnCode;
            _message = message;
            IsSuccess = isSuccess;
            Error = error;
            ReturnData = returnData;
            ReturnData = returnData;
        }

        public ResultReturn(Exception error)
        {
            Message = error.Message;
            IsSuccess = false;
        }

        /// <summary>
        ///     返回代码,自定义
        /// </summary>
        public int ReturnCode { set; get; }

        /// <summary>
        ///     返回的文本提示信息
        /// </summary>
        public string Message
        {
            set { _message = value; }
            get
            {
                if (string.IsNullOrWhiteSpace(_message))
                {
                    if (Error!=null)
                    {
                        return Error.Message;
                    }
                }
                else
                {
                    return _message;
                }

                return string.Empty;
            }
        }

        /// <summary>
        ///     表明函数执行是否成功
        /// </summary>
        public bool IsSuccess {protected  set; get; }

        /// <summary>
        ///     执行过程中出现错误时的错误对象
        /// </summary>
        public Exception Error { set; get; }

        /// <summary>
        ///     如果函数有返回值,则设置该属性
        /// </summary>
        public virtual Object ReturnData { set; get; }
    }

    public class ResultReturn<T> : ResultReturn
    {
        public ResultReturn(bool isSuccess,T returnData=default(T), string message = "", int returnCode = 0, Exception error = null) : base(isSuccess,returnData, message, returnCode, error)
        {
            ReturnCode = returnCode;
            ReturnData = returnData;
        }

        public new  T ReturnData { set; get; }
    }

    /// <summary>
    ///     函数执行成功的返回值
    /// </summary>
    [Serializable]
    public class SuccessResuleReturn : ResultReturn
    {
        private static SuccessResuleReturn _defaultValue=new SuccessResuleReturn();
        public SuccessResuleReturn() : base(true,null,string.Empty, 0,null)
        {
        }

        public SuccessResuleReturn(object returnData):base(true,returnData,string.Empty,0,null){}

        /// <summary>
        ///     返回默认构建的成功标识对象,注意使用Default属性返回的对象是全局唯一,对该对象内的属性做修改时,将会影响全局
        /// </summary>
        public SuccessResuleReturn Default { get { return _defaultValue; } }
    }

    public class  SuccessResuleReturn<T>:SuccessResuleReturn
    {
        public SuccessResuleReturn():base()
        {
            ReturnData = default(T);
        }

        public SuccessResuleReturn(T returnData) : base(returnData)
        {
            ReturnData = returnData;
        }

        public new T ReturnData { set; get; }
    }


    public class FailResultReturn : ResultReturn
    {
        private static FailResultReturn _defaultValue = new FailResultReturn("");
        public FailResultReturn(Exception error)
            : base(false, error, string.Empty, 0, null)
        {
        }

        public FailResultReturn(string errorMessage):base(false,null,errorMessage,0,new Exception(errorMessage))
        {}

        public FailResultReturn Default { get { return _defaultValue; } } 
    }

    
}
